#!/bin/bash

set -e -o pipefail
export JAVA_HOME=/usr/java/jdk11.0.1
export PATH=$JAVA_HOME/bin:$PATH

source "`dirname "$0"`"/elasticsearch-env

IFS=';' read -r -a additional_sources <<< "$ES_ADDITIONAL_SOURCES"
for additional_source in "${additional_sources[@]}"
do
  source "$ES_HOME"/bin/$additional_source
done

IFS=';' read -r -a additional_classpath_directories <<< "$ES_ADDITIONAL_CLASSPATH_DIRECTORIES"
for additional_classpath_directory in "${additional_classpath_directories[@]}"
do
  ES_CLASSPATH="$ES_CLASSPATH:$ES_HOME/$additional_classpath_directory/*"
done

# use a small heap size for the CLI tools, and thus the serial collector to
# avoid stealing many CPU cycles; a user can override by setting ES_JAVA_OPTS
ES_JAVA_OPTS="-Xms4m -Xmx64m -XX:+UseSerialGC ${ES_JAVA_OPTS}"

if [ -x "$JAVA_HOME/bin/java" ]; then
    JAVA="/usr/java/jdk11.0.1/bin/java"
else
    JAVA=`which java`
fi

exec \
  "$JAVA" \
  "$XSHARE" \
  $ES_JAVA_OPTS \
  -Des.path.home="$ES_HOME" \
  -Des.path.conf="$ES_PATH_CONF" \
  -Des.distribution.flavor="$ES_DISTRIBUTION_FLAVOR" \
  -Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
  -cp "$ES_CLASSPATH" \
  "$ES_MAIN_CLASS" \
  "$@"
